//
//  VISTAPictureDetailVC.m
//  看杂志
//
//  Created by zcj on 15-12-14.
//  Copyright (c) 2015年 . All rights reserved.
//

#import "VISTAPictureDetailVC.h"
#import "VISTACustomTabBar.h"
#import "Grouble.h"
#import "VISTAPictureViewModel.h"
#import "SelectFindHttp.h"

@interface VISTAPictureDetailVC ()
{
    UIActivityIndicatorView *_aiv;
    
}
@end

@implementation VISTAPictureDetailVC

- (void)viewWillAppear:(BOOL)animated
{
    [self.tabBarController.tabBar removeFromSuperview];
    UIView *tabr = (VISTACustomTabBar *)[self.tabBarController.view viewWithTag:757];
    [tabr setHidden:YES];
    
}
- (void)viewWillDisappear:(BOOL)animated
{
    UIView *tabr = (VISTACustomTabBar *)[self.tabBarController.view viewWithTag:757];
    [tabr setHidden:NO];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:1];
    VISTAPictureViewModel * model =self.dataSourceArray[_indexPathRow];
    [self loadWithGid:model.gid];
    [self configNavi];
    [self addBottomView];
    [self configView];
    
}
- (void)loadWithGid:(NSString *)gid
{
    NSString *path = [NSString stringWithFormat:@"http://ktx.cms.palmtrends.com/api_v2.php?action=picture&sa=&uid=10067567&mobile=iphone5&offset=0&count=15&gid=%@&moblie=iphone5&e=40dab97d773e7860febfc897c04824e2&uid=10067567&pid=10053&mobile=iphone5&platform=i",gid];
    NSLog(@"gid = %@",gid);
    //状态栏网络活动指示器
    [_aiv startAnimating];
    
    [SelectFindHttp postWirhUrl:path parms:nil success:^(id json) {
        NSLog(@"%@",json);
        NSArray *list = json[@"list"];
        self.textLabel.text  = [NSString stringWithFormat:@"       %@",list[0][@"des"]];
        self.titleLabel.text = [NSString stringWithFormat:@"    %@",list[0][@"title"]];
        [self.imageView setImageWithURL:[NSURL URLWithString:list[0][@"small_icon"]] placeholderImage:[UIImage imageNamed:@"图片点击放大缺省图.png"]];
        [_aiv stopAnimating];
    } failture:^(id error) {
        
    }];
    [_aiv stopAnimating];

    UIApplication *app = [UIApplication sharedApplication];
    app.networkActivityIndicatorVisible = !app.networkActivityIndicatorVisible;
}
- (void)configNavi
{
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"NAVI.png"] forBarMetrics:UIBarMetricsDefault];
    //定制导航栏左按钮
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 50, 32);
    [leftButton setImage:[UIImage imageNamed:@"返回1.png"] forState:UIControlStateNormal];
    [leftButton setImage:[UIImage imageNamed:@"返回2.png"] forState:UIControlStateHighlighted];
    [leftButton addTarget:self action:@selector(pop) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = leftItem;
}
- (void)pop
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)addBottomView
{
    VISTACustomWebBar *webBar = [[VISTACustomWebBar alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height - 35, self.view.frame.size.width, 35)];
    webBar.delegate = self;
    [self.view addSubview:webBar];
}
#pragma mark VISTACustomWebBarDelegate
- (void)customWebBar:(VISTACustomWebBar *)webBar didClickButton:(UIButton *)button WithButtonIndex:(NSUInteger)index
{
    switch (index) {
        case 0:
            if (_indexPathRow > 0) {
                VISTAPictureViewModel * model =self.dataSourceArray[_indexPathRow--];
                
                [self loadWithGid:model.gid];
            }else if (_indexPathRow == 0){
                NSLog(@"没有上一页");
                return ;
            }
            break;
        case 1:
            
            break;
        case 2:
            
            break;
        case 3:
            
            break;
        case 4:
            if (_indexPathRow < self.dataSourceArray.count - 1) {
                VISTAPictureViewModel * model =self.dataSourceArray[_indexPathRow++];
                
                [self loadWithGid:model.gid];
            }else if (_indexPathRow == self.dataSourceArray.count){
                NSLog(@"没有下一页");
                return ;
            }
            break;
            
        default:
            break;
    }
}

- (void)configView
{
    CGFloat width = self.view.frame.size.width;
    CGFloat height = self.view.frame.size.height;
    
    
    self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, width, height-35-100)];
    self.imageView.contentMode = UIViewContentModeScaleAspectFit;
    
    UIView *textView = [[UIView alloc]initWithFrame:CGRectMake(0, height-35-100, width, 100)];
    [textView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@""]]];
    [UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:1];
    
    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(10, 35, width-20, 100-35)];
    [UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:1];
    scrollView.delegate = self;
    scrollView.contentSize = CGSizeMake(width-20, 100);
    scrollView.contentOffset = CGPointMake(0, 0);
    
    self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, width, 35)];
    self.titleLabel.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"ad_title_bg.png"]];
    self.titleLabel.textColor = [UIColor whiteColor];
    
    
    self.textLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, width-20, 100)];
    self.textLabel.font = [UIFont systemFontOfSize:11];
    self.textLabel.numberOfLines = 0;
    self.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
    self.textLabel.textColor = [UIColor whiteColor];
    
    
    [self.view addSubview:self.imageView];
    [scrollView addSubview:self.textLabel];
    [textView addSubview:self.titleLabel];
    [textView addSubview:scrollView];
    [self.view addSubview:textView];
    [self createAIV];
}
/**
 *  活动指示器
 */
- (void)createAIV
{
    _aiv = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    _aiv.center = self.view.center;
    _aiv.color = [UIColor redColor];
    [self.view addSubview:_aiv];
}


@end
